/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.data;

import aptech.semester.code.conn.MyLib;
import aptech.semester.code.entity.BankEntity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author vi
 */
public class BankList {
    private static final String QUERY_ALL_BANK = "SELECT * FROM BANK";

    public List<BankEntity> getAllBank() {
        List<BankEntity> listBank = new ArrayList<>();
        
        try {          
            Connection cn = MyLib.getCN();
            if(cn != null) {
                Statement st = cn.createStatement();
                 ResultSet rs = st.executeQuery(QUERY_ALL_BANK);
                 
                while(rs.next()) {
                    BankEntity bank = new BankEntity();
                    bank.setBankId(rs.getInt("ID"));
                    bank.setBankName(rs.getString("BANK_NAME"));
                    bank.setAddress(rs.getString("ADDRESS"));
                    bank.setDescription(rs.getString("DESCRIPTION"));
                     
                    listBank.add(bank);
                }
                rs.close();
                cn.close();
            }           
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return listBank;
    }
        //-------------------------INSERT-------------------------
    
    public boolean insert(BankEntity b){
        try{
            // 1. Tao ket noi
            Connection cn = MyLib.getCN();
            // 2. Tao doi tuong chua lenh insert sql
            String sql = "Insert BANK values(?,?,?)";
            PreparedStatement pst = cn.prepareStatement(sql);
           // pst.setInt(1, b.Id);
            pst.setString(1, b.getBankName());
            pst.setString(2, b.getAddress());
            pst.setString(3, b.getDescription());
            // 3. Cho thi hanh cau lenh sql
            int n = pst.executeUpdate();
            pst.close();
            cn.close();
            return(n>0);
        }catch(Exception e){
          JOptionPane.showMessageDialog(null, "Bank name existed.Please enter different name!");
             return false;
        }
        
       
        
    }
    
    //-------------------------UPDATE-------------------------
    
    public boolean update(BankEntity b){
        try{
            // 1. Tao ket noi
            Connection cn = MyLib.getCN();
            // 2. Tao doi tuong chua lenh update sql
            String sql = "Update BANK set BANK_NAME = ?, ADDRESS = ?, DESCRIPTION = ? where ID = ?";
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, b.getBankName());
            pst.setString(2, b.getAddress());
            pst.setString(3, b.getDescription());
            pst.setInt(4, b.getBankId());
            // 3. Cho thi hanh cau lenh sql
            int n = pst.executeUpdate();
            pst.close();
            cn.close();
            return(n>0);
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }
    
    //-------------------------DELETE-------------------------
    public boolean delete(int Id){
        try{
           // 1.Tao ket noi
            Connection cn = MyLib.getCN();
           // 2.Tao doi tuong chua lenh delete sql
            String sql = "Delete from BANK where ID = ?";
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setInt(1, Id);
           // 3.Cho thi hanh cau lenh sql
            int n = pst.executeUpdate();
            pst.close();
            cn.close();
            return(n>0);
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Can not delete beccase of having employee belong this bank!");
            
        }
        
        return false;
    }
}
